<?php

namespace App\Http\Controllers;

use App\Models\Archive;
use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function bizhi(Request $request)
    {
        $category = Category::where('slug', 'bizhi')->first();
        $archives = $this->getArchives($category);
        return view('archives.index', compact('archives', 'category'));
    }

    public function touxiang(Request $request)
    {
        $category = Category::where('slug', 'touxiang')->first();
        $archives = $this->getArchives($category);
        return view('archives.index', compact('archives', 'category'));
    }

    public function meitu(Request $request)
    {
        $category = Category::where('slug', 'meitu')->first();
        $archives = $this->getArchives($category);
        return view('archives.index', compact('archives', 'category'));
    }

    private function getArchives($category)
    {
        return Archive::where('category_id', $category->id)->whereHas('files')->with(['files', 'tagged'])->withCount('likes')->latest()->paginate();
    }
}
